Skip to content

Rely on typeshed/stubs for slice typing#21401

Merged
ilevkivskyi merged 2 commits intopython:masterfrom
ilevkivskyi:enable-slice
May 4, 2026
Merged

Rely on typeshed/stubs for slice typing#21401
ilevkivskyi merged 2 commits intopython:masterfrom
ilevkivskyi:enable-slice

Conversation

@ilevkivskyi
Copy link
Copy Markdown
Member

Fixes #2410

slice has been generic in typeshed for more than a year now. Also all builtin collections have precise slice[SupportsIndex | None] types for __getitem__(). So, I don't think we need to be "policing" slice anymore, it should be really responsibility of the stub maintainers now.

After some searching, I see there are still many plain slice annotations in the wild, so this will be a trade-off of false positives to false negatives, but I think this is a right trade-off, since status quo causes issues for pandas dataframes and similar APIs.

I also considered some "intermediate" solutions, where we would still enforce SupportsIndex in some situations, but can't find anything that wouldn't be overly complicated.

@ilevkivskyi
Copy link
Copy Markdown
Member Author

cc @Dr-Irv

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

sympy (https://github.com/sympy/sympy)
+ sympy/stats/stochastic_process_types.py:1255: error: Unused "type: ignore" comment  [unused-ignore]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/tests/indexes/string/test_indexing.py:152: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:153: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:154: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:155: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:156: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:157: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:159: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:160: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:161: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:162: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:163: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:164: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:165: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:166: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:167: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:410: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:478: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:480: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:482: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:485: error: Unused "type: ignore" comment  [unused-ignore]

xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_dataarray.py:1327: error: Unused "type: ignore" comment  [unused-ignore]
+ xarray/tests/test_dataarray.py:1346: error: Unused "type: ignore" comment  [unused-ignore]

freqtrade (https://github.com/freqtrade/freqtrade)
- freqtrade/strategy/strategy_helper.py:107: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/strategy/strategy_helper.py:108: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:119: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:120: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:135: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[int | str | str_ | Timestamp | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...] | Callable[[DataFrame], Never], int | str | str_ | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...]]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[tuple[IndexOpsMixin[Any, Any] | Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any] | Sequence[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]] | slice[Any, Any, Any], ...], Hashable]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:151: error: Slice index must be an integer, SupportsIndex or None  [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[int | str | str_ | Timestamp | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...] | Callable[[DataFrame], Never], int | str | str_ | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...]]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[tuple[IndexOpsMixin[Any, Any] | Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any] | Sequence[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]] | slice[Any, Any, Any], ...], Hashable]"  [index]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Slice index must be an integer, SupportsIndex or None  [misc]

@ilevkivskyi
Copy link
Copy Markdown
Member Author

mypy_primer is as expected.

@Dr-Irv
Copy link
Copy Markdown

Dr-Irv commented May 4, 2026

For pandas, we're happy to remove those # type: ignore lines once this is released.

@ilevkivskyi
Copy link
Copy Markdown
Member Author

@Dr-Irv cc was more to use more precise slice type(s) if possible. Right now there are a bunch of plain slice, which is slice[Any]. If this is intended, then an explicit slice[Any] would still be better, as it would be an explicit "this is too dynamic to type" rather than something that looks like an accidental omission.

@ilevkivskyi ilevkivskyi merged commit 0007c52 into python:master May 4, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the enable-slice branch May 4, 2026 21:27
@Dr-Irv
Copy link
Copy Markdown

Dr-Irv commented May 4, 2026

@Dr-Irv cc was more to use more precise slice type(s) if possible. Right now there are a bunch of plain slice, which is slice[Any]. If this is intended, then an explicit slice[Any] would still be better, as it would be an explicit "this is too dynamic to type" rather than something that looks like an accidental omission.

In the pandas source, we have

class IndexSlice:
    def __getitem__(self, arg):
       return arg

so that IndexSlice[3:4, "a:b"] works, so we don't want to do any precise slice types. But we could look into typing arg in this case to be slice[Any] to fix this issue. And maybe we can do that now.

How can we track when this is released?

@ilevkivskyi
Copy link
Copy Markdown
Member Author

How can we track when this is released?

This will be included in v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected "error: Slice index must be an integer or None"

3 participants